<!--
/**
 * Created by PhpStorm.
 * User: Devmc
 * Date: 2021/4/25
 * Time: 1:05
 */
-->
<template>
  <div class="profile">
    <div class="form-content">
      <el-form ref="form" :model="form" :rules="formRules" label-width="100px">
        <el-form-item label="头像">
          <el-upload
              drag
              class="avatar-uploader"
              name="file"
              :action="this.$store.state.api + '/app/order/PictureUpload'"
              :show-file-list="false"
              :on-success="handleSuccess"
              :before-upload="beforeUpload">
            <el-avatar
                v-if="imgUrl"
                :src="imgUrl"
                :size="178"
                shape="square"
                class="avatar">
            </el-avatar>
            <i v-else class="el-icon-plus icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="昵称" prop="userName" key="userName">
          <el-input v-model="form.userName" placeholder="请输入昵称"></el-input>
        </el-form-item>

        <el-form-item label="姓名" prop="tuserName" key="tuserName">
          <el-input v-model="form.tuserName" placeholder="请输入姓名"></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="phone" key="phone">
          <el-input v-model="form.phone" placeholder="请输入电话"></el-input>
        </el-form-item>

        <el-form-item label="身份证号" prop="info" key="info">
          <el-input v-model="form.info" placeholder="请输入身份证号"></el-input>
        </el-form-item>

        <el-form-item label="住址">
          <el-input v-model="form.address" prop="contactsName" placeholder="请输入住址"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password" key="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item label="生日">
          <el-date-picker
              type="date"
              placeholder="请选择生日"
              :value-format="'yyyy-MM-dd'"
              v-model="form.birthday">
          </el-date-picker>
        </el-form-item>

        <el-row class="action-button">
          <el-col :span="14" align="right">
            <el-button type="danger" icon="el-icon-close" @click="back">取消</el-button>
            <el-button type="primary" icon="el-icon-edit" @click="confirm">保存</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import modelUser from "../../models/user";

export default {
  name: "MainProfile",
  props: {},
  data() {
    return {
      // 数据
      form: {
        /*"address": "string",
        "birthday": "string",
        "img": "string",
        "info": "string",
        "password": "string",
        "phone": "string",
        "role": "string",
        "tuserName": "string",
        "userId": 0,
        "userName": "string",
        "workPlace": "string"*/
      },
      // 头像
      imgUrl: '',

      // 校验规则
      formRules: {
        userName: this.$store.state.formRule.userName,
        tuserName: this.$store.state.formRule.name,
        password: this.$store.state.formRule.password,
        phone: this.$store.state.formRule.phone,
        info: this.$store.state.formRule.idCard,
      },
    }
  },
  created() {
    this.getUserData();
  },
  mounted() {

  },
  methods: {
    /**
     * 获取信息
     */
    getUserData() {
      modelUser.getUserDataById(this.$store.state.userData.userId).then((response) => {
        // 无数据
        if (response == null ||
            (this.editType == 'resident' && response.role != '0') ||
            (this.editType == 'staff' && response.role != '1' && response.role != '2')) {
          this.$message({
            type: 'error',
            message: '加载数据失败！',
            onClose: () => {
              this.back();
            }
          });
        }
        else {
          this.form = response;
          this.imgUrl = response.img;
        }
      });
    },

    /**
     * 保存
     */
    confirm() {
      this.$refs['form'].validate(yes => {
        if (yes) {
          this.axios.put(this.$store.state.api + '/user/update', [{
            birthday: this.form.birthday || '',
            info: this.form.info || '',
            phone: this.form.phone || '',
            role: this.form.role || '',
            tuserName: this.form.tuserName || '',
            userName: this.form.userName || '',
            img: this.form.img || '',
            workPlace: this.form.workPlace + '' || '',
            address: this.form.address || '',
            password: this.form.password || '',
            userId: this.form.userId
          }]).then((response) => {
            this.$message({
              type: response.data.data ? 'success' : 'error',
              message: response.data.data ? '修改成功！' : '修改失败！',
              duration: 2000
            });
          }).catch((error) => {
            console.log(error);
          });
        }
      })
    },

    /**
     * 返回
     */
    back() {
      this.$router.replace('/');
    },


    /**
     * 上传成功
     *
     * @param res
     */
    handleSuccess(res) {
      console.log(res);
      this.form.img = res.data;
      this.imgUrl = res.data;
    },

    /**
     * 检测图片大小
     *
     * @param file
     * @return {boolean}
     */
    beforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 <= 3;
      if (!isLt2M) {
        this.$message.error("头像大小不能超过3MB!");
      }
      return isLt2M;
    },
  },
  watch: {}
}
</script>

<style lang="scss">

</style>